<?php

namespace App\Http\Controllers\Home;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;

class AddressController extends Controller
{
    /**
     * 保存地址数据,将地址数据写入数据库
     * @param Request $request
     */
    public function saveaddress(Request $request){
        $data = $request->all();

//        'action': action,
        // 'useremail': useremail,
//                    'getmanfield': getmanfield,
//                    'sexfield': sexfield,
//                    'addressfield': addressfield,
//                    'detailaddressfield': detailaddressfield,
//                    'phonefield': phonefield,
        if ($data['address_id'] == 0) {
            $id = DB::table('addresses')->insertGetId(
                [
                    'user_email' => $data['useremail'],
                    'getname' => $data['getmanfield'],
                    'sex' => $data['sexfield'],
                    'address' => $data['addressfield'],
                    'position' => $data['detailaddressfield'],
                    'phone' => $data['phonefield']
                ]
            );
//            dd

            return response()->json($id);

        } elseif ($data['address_id'] != 0) {
            DB::table('addresses')
                ->where('id', $data['address_id'])
                ->update(
                [
                    'user_email' => $data['useremail'],
                    'getname' => $data['getmanfield'],
                    'sex' => $data['sexfield'],
                    'address' => $data['addressfield'],
                    'position' => $data['detailaddressfield'],
                    'phone' => $data['phonefield']
                ]
            );
            return response()->json( $data['address_id']);
        }
    }
}
